package com.changgou.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.pay.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import entity.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述
 *
 * @author ljh
 * @version 1.0
 * @package com.changgou.pay.service.impl *
 * @Date 2019-12-8
 * @since 1.0
 */
@Service
public class WeixinPayServiceImpl implements WeixinPayService {

    @Value("${weixin.notifyurl}")
    private String notifyurl;

    @Value("${weixin.partnerkey}")
    private String partnerkey;

    @Override
    public Map<String, String> createNative(Map<String, String> parameter) {
        try {
            //1.用map来组装参数
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("appid", "wx8397f8696b538317");
            paramMap.put("mch_id", "1473426802");
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paramMap.put("body", "畅购");
            paramMap.put("out_trade_no", parameter.get("out_trade_no"));
            paramMap.put("total_fee", parameter.get("total_fee"));
            //多加一个自定义附件数据


       /*     parameter.get();//获取到页面传递过来的交换机数据
            parameter.get();//获取到页面传递过来的队列数据
            parameter.get();//获取到页面传递过来的routingkey*/

            String s = JSON.toJSONString(parameter);//parmater就会有用户名
            paramMap.put("attach", s);


            paramMap.put("notify_url", notifyurl);
            paramMap.put("trade_type", "NATIVE");//扫描支付类型
            //签名 todo (在转换XML 自动添加)
            //2.将map转换变成xml
            String paramXml = WXPayUtil.generateSignedXml(paramMap, partnerkey);
            //3.调用httpclient的方法 模拟浏览器发送请求
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");//相当于打开浏览器 输入要访问的url
            httpClient.setHttps(true);//设置为https请求
            httpClient.setXmlParam(paramXml);//设置请求体(XML)
            httpClient.post();//相当于按下回车键 POST请求
            //4.调用httpclient的方法 模拟浏览器接收响应(XML)
            String content = httpClient.getContent();//获取响应结果
            System.out.println(content);//字符串
            //5.将XML 转成MAP
            //6.map中有支付二维码的链接数据
            Map<String, String> map = WXPayUtil.xmlToMap(content);

            //7.封装返回
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("code_url", map.get("code_url"));
            resultMap.put("out_trade_no", parameter.get("out_trade_no"));
            resultMap.put("total_fee", parameter.get("total_fee"));

            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }

    @Override
    public Map<String, String> queryStatus(String out_trade_no) {
        try {
            //1.用map来组装参数
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("appid", "wx8397f8696b538317");
            paramMap.put("mch_id", "1473426802");
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paramMap.put("out_trade_no", out_trade_no);
            //签名 todo (在转换XML 自动添加)
            //2.将map转换变成xml
            String paramXml = WXPayUtil.generateSignedXml(paramMap, partnerkey);

            //3.调用httpclient的方法 模拟浏览器发送请求
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");//相当于打开浏览器 输入要访问的url
            httpClient.setHttps(true);//设置为https请求
            httpClient.setXmlParam(paramXml);//设置请求体(XML)
            httpClient.post();//相当于按下回车键 POST请求
            //4.调用httpclient的方法 模拟浏览器接收响应(XML)
            String content = httpClient.getContent();//获取响应结果
            System.out.println(content);//字符串
            //5.将XML 转成MAP
            //6.map中有支付二维码的链接数据
            Map<String, String> resultMap = WXPayUtil.xmlToMap(content);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }
}
